﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;

namespace AssemblyExplorer
{
    public struct StrategySettings
    {
        public const string XmlName = "StrategySettings";
        public StrategyOption StrategyOption;
        public string PluginAssembly;
        public string PluginType;
        public string PluginState;
        public string UserProvidedCode;

        public string GetXml()
        {
            StringBuilder sb = new StringBuilder();
            XmlWriter writer = XmlWriter.Create(sb);
            writer.WriteStartElement(StrategySettings.XmlName);
            writer.WriteElementString("StrategyOption", this.StrategyOption.ToString());
            writer.WriteElementString("PluginAssembly", this.PluginAssembly);
            writer.WriteElementString("PluginType", this.PluginType);
            writer.WriteElementString("PluginState", this.PluginState);
            writer.WriteElementString("UserProvidedCode", this.UserProvidedCode);
            writer.WriteEndElement();
            writer.Close();
            return sb.ToString();
        }

        public void LoadXml(string xml)
        {
            if (String.IsNullOrEmpty(xml)) return;
            TextReader txtReader = new StringReader(xml);
            XmlReader reader = XmlReader.Create(txtReader);
            reader.ReadToFollowing(StrategySettings.XmlName);
            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    L1:
                    switch (reader.LocalName)
                    {
                        case "StrategyOption":
                            this.StrategyOption = (StrategyOption)Enum.Parse(typeof(StrategyOption), reader.ReadElementContentAsString());
                            break;
                        case "PluginAssembly":
                            this.PluginAssembly = reader.ReadElementContentAsString();
                            break;
                        case "PluginType":
                            this.PluginType = reader.ReadElementContentAsString();
                            break;
                        case "PluginState":
                            this.PluginState = reader.ReadElementContentAsString();
                            break;
                        case "UserProvidedCode":
                            this.UserProvidedCode = reader.ReadElementContentAsString();
                            break;
                        default:
                            reader.Read();
                            break;
                    }
                    if (reader.NodeType == XmlNodeType.Element) goto L1;
                }
                else if (reader.NodeType == XmlNodeType.EndElement && reader.LocalName == StrategySettings.XmlName)
                {
                    break;
                }
            }
        }
    }
}
